// IniFile.cpp: implementation of the CIniReader class.
//
//////////////////////////////////////////////////////////////////////


//#include "Readini.h"
#include "INI.h"
#include <string>
#include <Windows.h>
#include <tchar.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////



// Used to retrieve a value give the section and key
std::wstring CIniReader::getKeyValue(std::wstring strKey,std::wstring strSection)
{
	WCHAR ac_Result[255];
	//WCHAR ret=WCHAR("");

	// Get the info from the .ini file	
	m_lRetValue = GetPrivateProfileStringW(strSection.c_str(),strKey.c_str(),__W(""),ac_Result, 255, m_strFileName.c_str());	

	std::wstring strResult(ac_Result);
	return strResult;
}

// Used to add or set a key value pair to a section
long CIniReader::setKey(std::wstring strValue, std::wstring strKey, std::wstring strSection)
{
	m_lRetValue = WritePrivateProfileStringW(strSection.c_str(), strKey.c_str(), 
                             strValue.c_str(), m_strFileName.c_str());

	return m_lRetValue;
}

// Used to find out if a given section exists
bool CIniReader::sectionExists(std::wstring strSection)
{
	WCHAR ac_Result[100];	
	std::wstring csAux;
	
	// Get the info from the .ini file	
	m_lRetValue = GetPrivateProfileStringW(strSection.c_str(),NULL,
		__W(""),ac_Result, 90, m_strFileName.c_str());
	// Return if we could retrieve any info from that section
	return (m_lRetValue > 0);
}

// Used to retrieve all of the  section names in the ini file
std::list<std::wstring>* CIniReader::getSectionNames()  //returns collection of section names
{
	WCHAR ac_Result[2000];
	m_sectionList->clear();
	
	m_lRetValue = GetPrivateProfileSectionNamesW(ac_Result,2000,m_strFileName.c_str());
	
	std::wstring strSectionName;
	for(int i=0; i<m_lRetValue; i++)
	{
		if(ac_Result[i] != '\0') {
			strSectionName = strSectionName + ac_Result[i];
		} else {
			if(strSectionName != __W("")) {
				m_sectionList->push_back(strSectionName);
			}
			strSectionName = __W("");
		}
	}

	return m_sectionList;
}

// Used to retrieve all key/value pairs of a given section.  
std::list<std::wstring>* CIniReader::getSectionData(std::wstring strSection)  
{
	WCHAR ac_Result[2000];  //change size depending on needs
	m_sectionDataList->clear();
	m_lRetValue = GetPrivateProfileSectionW(strSection.c_str(), ac_Result, 2000, m_strFileName.c_str());

	std::wstring strSectionData;
	for(int i=0; i<m_lRetValue; i++)
	{
		if(ac_Result[i] != '\0') {
			strSectionData = strSectionData + ac_Result[i];
		} else {
			if(strSectionData != __W("")) {
				m_sectionDataList->push_back(strSectionData);
			}
			strSectionData = __W("");
		}
	}

	return m_sectionDataList;
}

void CIniReader::setINIFileName(std::wstring strINIFile)
{
	m_strFileName = strINIFile;
}
